<template>
  <div class="commentList">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <CommentItem
        v-for="(comment, index) in list"
        :key="index"
        :comment="comment"
        @reply-click="$emit('reply-click', $event)"
      />
    </van-list>
  </div>
</template>
<script>
import { getCommentsList } from '@/api/comment'
import CommentItem from '../components/comment-item'
export default {
  name: 'commentList',
  data () {
    return {
      // list: [],
      loading: false,
      finished: false,
      page: 1
    }
  },
  created () {},
  methods: {
    async onLoad () {
      // 异步更新数据
      const { data } = await getCommentsList({
        type: this.type, // 评论类型,a表示文章评论,c表示评论回复
        source: this.source, // 文章id或者评论id
        page: this.page
      })
      this.loading = false
      if (data.data[0]) {
        this.list.push(...data.data)
        this.page = data.page
      } else {
        this.finished = true
      }
    }
  },
  components: { CommentItem },
  props: {
    source: {
      type: [Number, String],
      required: true
    },
    list: {
      type: Array,
      default: () => {
        return []
      }
    },
    type: {
      type: String,
      default: 'a'
    }
  },
  watch: {},
  computed: {},
  mounted () {}
}
</script>

<style lang="less" scoped></style>
